﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using CPETwit.Data.Entity;
using CPETwit.Data.Manager;

namespace CPETwit.Web.Business
{
    public class UtilisateurBusiness
    {
        UserManager _globalUserManager;
        private AbonnementManager _abonnementManager;

        public UtilisateurBusiness()
        {
            _globalUserManager = new UserManager();
            _abonnementManager = new AbonnementManager();
        }


        public User GetUser(string login)
        {
            IEnumerable<User> listeUser = _globalUserManager.GetUserByLogin(login);
            if (listeUser != null && listeUser.Count() == 1)
                return listeUser.First();
            return null;
        }

        public IEnumerable<User> GetUsersByText(string text)
        {
            return _globalUserManager.GetUsersByText(text);
        }

        public User getUserFromId(string _id)
        {
            long[] users = new long[1];
            users[0] = long.Parse(_id);
            IEnumerable<User> listeUser = _globalUserManager.GetUsersById(users);

            if (listeUser.Count() == 1)
            {
                return listeUser.First();
            }
            else
            {
                return null;
            }
        }

        public IEnumerable<User> GetFollowers(User user)
        {
            IEnumerable<User> listeFollower = _globalUserManager.GetFollowers(user.id);
            return listeFollower;
        }

        public IEnumerable<User> GetUnFollowers(User user)
        {
            IEnumerable<User> listeUnFollower = _globalUserManager.GetUnFollowers(user.id);
            return listeUnFollower;
        }

        public IEnumerable<User> GetPublishers(User user)
        {
            IEnumerable<User> listePublishers = _globalUserManager.GetPublishers(user.id);
            return listePublishers;
        }

        public IEnumerable<User> GetUnPublishers(User user)
        {
            IEnumerable<User> listeUnPublishers = _globalUserManager.GetUnPublishers(user.id);
            return listeUnPublishers;
        }

        public User createUser(string login, string password, string email)
        {

            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email))
            {
                return null;
            }

            User userToCreate = new User
            {
                login = login,
                password = password,
                email = email,
                name = login
            };

            _globalUserManager.CreateUser(userToCreate);
            _globalUserManager.FollowUser(userToCreate, userToCreate);

            return userToCreate;
        }

        public void updateUser(User u)
        {
            _globalUserManager.UpdateUser(u);
        }

        public bool deleteUser(string login)
        {
            IEnumerable<User> listUserToDelete = _globalUserManager.GetUserByLogin(login);
            if (listUserToDelete.Count() == 1)
            {
                _globalUserManager.DeleteUser(listUserToDelete.First());
                return true;
            }
            else
            {
                throw new Exception("Le compte n'a pas été supprimé car le compte n'a pas été trouvé");
            }

        }

        public bool IsEmailValid(string email)
        {
            return Regex.IsMatch(email,
                  @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                  @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$") &&
                  _globalUserManager.IsEmailValid(email);
        }
        public bool IsUserNameValid(string login)
        {
            return _globalUserManager.IsUserNameValid(login);
        }

        public IEnumerable<User> GetTweetOwner(Tweet t)
        {
            return _globalUserManager.GetTweetOwner(t);
        }

        public IEnumerable<User> RechercheUser(Dictionary<string, string> filtres)
        {
            return _globalUserManager.RechercheUser(filtres);
        }

        public void Follow(User follower, User publisher)
        {
            Abonnement abo = Abonnement.CreateAbonnement(publisher.id, follower.id, false);
            _abonnementManager.CreateAbonnement(abo);
            _globalUserManager.UpdateUser(follower);
            _globalUserManager.UpdateUser(publisher);
        }

        public void Unfollow(User follower, User publisher)
        {
            Abonnement abo = _abonnementManager.GetAbonnement(follower.id, publisher.id);
            _abonnementManager.DeleteAbonnement(abo);
        }
    }
}
